package com.bawei.detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.detail.databinding.ItemDetailLayoutBinding
import com.bawei.image.ImageLoadUtil
import com.bawei.image.ImageOptions

/**
 * Author: Jason Zhang
 * Date: 2024/3/9
 * Desc:
 */
class DetailPagingAdapter:  PagingDataAdapter<MovieVideoEntity, DetailPagingAdapter.DetailViewHolder>(DetailEntityComparator()){

    private var itemClickListener: ItemClickListener? = null

    fun setItemClickListener(listener: ItemClickListener) {
        itemClickListener = listener
    }

    inner class DetailViewHolder(val dataBind: ItemDetailLayoutBinding) : RecyclerView.ViewHolder(dataBind.root)

    override fun onBindViewHolder(holder: DetailViewHolder, position: Int) {
        val entity = getItem(position)
        if(entity != null){
            //加载图片
            ImageLoadUtil.with(holder.dataBind.root.context)
                .setOptions(ImageOptions.Builder.isCircle(false).build())
                .loadUrl(entity.videomainimag)
                .into(holder.dataBind.ivMovieImg)

            holder.dataBind.tvMovieDesc.text = entity.description
            holder.dataBind.tvPlayCount.text = "${entity.favoritecount + 321}次播放"
            holder.dataBind.root.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position, entity)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailViewHolder {
        return DetailViewHolder(ItemDetailLayoutBinding.inflate(LayoutInflater.from(parent.context), parent,false))
    }

    interface ItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity)
    }

}